package com.bd.scm.module.utils;

import com.bd.scm.domain.dto.user.PersonnelFilesDto;
import com.bd.scm.module.user.service.IDtUserService;
import com.bd.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class ApproveUtil {

    @Autowired
    private IDtUserService dtUserService;

    /**
     * 获取一级审批人
     * @param operator
     * @param approverId
     * @param applyType
     * @return
     */
    public List<String> getFirstApprover(Long operator, Long approverId, int applyType) {
        List<String> approverList = new ArrayList<>();
        List<PersonnelFilesDto> personnelFilesDtoList1;

        if (StringUtils.isEmpty(String.valueOf(approverId))) {
            approverId = 0L;
        }
        //一级审批人
        if (approverId == 0) {

            if(applyType == 1){
                //个人申请获取一级审批人
                personnelFilesDtoList1 = dtUserService.getApproverList(String.valueOf(operator));
            }else{
                //批量申请获取一级审批人
                personnelFilesDtoList1 = dtUserService.getApproverListByBatch(String.valueOf(operator));
            }
            for (PersonnelFilesDto p : personnelFilesDtoList1) {
                approverList.add(p.getUserSerial().toString());
            }
        } else {
            approverList.add(String.valueOf(approverId));
        }

        return approverList;
    }

    /**
     * 获取二级审批人
     * @param operator
     * @param approverId
     * @param applyType
     * @return
     */
    public List<String> getSecondApprover(Long operator, Long approverId, int applyType) {
        List<String> approverList = new ArrayList<>();
        List<PersonnelFilesDto> personnelFilesDtoList2;

        if (StringUtils.isEmpty(String.valueOf(approverId))) {
            approverId = 0L;
        }

        //二级审批人
        if (approverId == 0) {
            if(applyType == 0){
                //个人申请获取二级审批人
                personnelFilesDtoList2 = dtUserService.getLabourUnionApproverList(String.valueOf(operator));
            }else{
                //批量申请获取二级审批人
                personnelFilesDtoList2 = dtUserService.getSecondListByBatch(String.valueOf(operator));
            }
            for (PersonnelFilesDto p : personnelFilesDtoList2) {
                approverList.add(p.getUserSerial().toString());
            }
        } else {
            approverList.add(String.valueOf(approverId));
        }

        return approverList;
    }
}
